<template>
    <div class="app-container">
        <el-form ref="form" :model="form" label-width="160px" :rules="rules">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="输入当前密码" prop="currentPassword">
                        <el-input type="password" v-model="form.currentPassword" placeholder="输入当前密码"/>
                    </el-form-item>
                    <el-form-item label="输入要修改的密码" prop="password">
                        <el-input type="password" v-model="form.password" placeholder="输入要修改的密码"/>
                    </el-form-item>
                    <el-form-item label="确认修改的密码" prop="confirmPassword">
                        <el-input type="password" v-model="form.confirmPassword" placeholder="确认修改的密码"/>
                    </el-form-item>
                    <div style="text-align: right;">
                        <el-button type="primary" @click="onSubmit">保 存</el-button>
                    </div>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>

  import { changePassword } from '@/api/password'

  function validatePass(value) {
    let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/
    return (!reg.test(value) || value.length < 6 || value.length > 12)
  }

  export default {
    data() {
      function validateFn(rule, value, callback) {
        if (validatePass(value)) {
          callback(new Error('请输入6-12位数字和字母组合'))
        } else {
          callback()
        }
      }

      const validateFn1 = (rule, value, callback) => {
        if (validatePass(value)) {
          callback(new Error('请输入6-12位数字和字母组合'))
        } else {
          if (this.form.password === this.form.currentPassword) {
            callback(new Error('新密码不能与旧密码一样'))
            return
          }
          if (this.form.confirmPassword !== '') {
            this.$refs.form.validateField('confirmPassword')
          }
          callback()
        }
      }

      const validateFn2 = (rule, value, callback) => {
        if (validatePass(value)) {
          callback(new Error('请输入6-12位数字和字母组合'))
        } else if (value !== this.form.password) {
          callback(new Error('确认密码和密码不相等'))
        } else {
          callback()
        }
      }
      return {
        form: {
          currentPassword: '',
          password: '',
          confirmPassword: ''
        },
        rules: {
          currentPassword: [
            { required: true, message: '当前密码不能为空', trigger: 'blur' },
            { validator: validateFn, trigger: 'blur' }
          ],
          password: [
            { required: true, message: '要修改的密码不能为空', trigger: 'blur' },
            { validator: validateFn1, trigger: 'blur' }
          ],
          confirmPassword: [
            { required: true, message: '确认密码不能为空', trigger: 'blur' },
            { validator: validateFn2, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      onSubmit() {
        this.$refs['form'].validate(async(valid) => {
          if (valid) {
            try {
              await changePassword(this.form)
              this.$message.success('修改密码成功!')
              await this.$store.dispatch('user/logout')
              this.$router.push(`/login?redirect=${this.$route.fullPath}`)
            } catch (e) {
              console.log(e)
            }
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style scoped>
    .line {
        text-align: center;
    }
</style>

